<link rel="stylesheet" href="/vendor/js/treegrid/css/jquery.treegrid.css">
<script src="/vendor/js/treegrid/js/jquery.treegrid.js"></script>
<script src="/vendor/js/treegrid/js/jquery.treegrid.bootstrap3.js"></script>
<div class="row">
    <div class="col-sm-3">
        <a href="{{url('/admin/category/edit')}}" class="btn btn-primary" >添加</a>
    </div>
</div>

<table id="categoryList" class="table list  table-bordered table-hover">
    <tr>
        <td>ID</td>
        <td>类目名称</td>
        <td>材质</td>
        <td>尺寸</td>
        <td>是否推荐</td>
        <td>状态</td>
        <td>操作</td>
    </tr>
    @foreach($categories as $key=>$category)
        <tr class="treegrid-{{$key}}">
            <td>{{$key}}</td>
            <td>{{$category['cate_name']}}</td>
            <td></td>
            <td></td>
            <td></td>
            <td>@if($category['status'] == 1)  <span class="label label-success">显示</span> @elseif ($category['status'] == 2)  <span class="label label-warning">隐藏</span> @endif</td>
            <td>
                <a href="{{url('admin/category/edit',['id'=>$category['id']])}}"
                   class="btn btn-primary">编辑</a>

                @if(!$category['is_has_goods'])
                    <button class="btn btn-primary delete" data-value="{{$category["id"]}}">删除
                    </button>
                    <button class="btn btn-primary toggle" data-value="{{$category["id"]}}"> @if($category['status'] == 1) 隐藏 @elseif($category['status'] == 2) 显示 @endif
                    </button>
                @endif

            </td>
        </tr>
        @if(!empty($category['children']))
            @foreach ($category['children'] as $child)
                <tr class="treegrid-{{$child['id']}} treegrid-parent-{{$category['id']}}">
                    <td>{{$child["id"]}}</td>
                    <td>{{$child["cate_name"]}}</td>
                    <td>{{$child["material"]}}</td>
                    <td>{{$child["size"]}}</td>
                    <td>@if($child["suggest_status"] == 1)
                            <span class="label label-success">推荐</span>
                        @else
                            <span class="label label-danger">不推荐</span>
                        @endif</td>
                    <td>@if($child['status'] == 1)  <span class="label label-success">显示</span> @elseif ($child['status'] == 2)  <span class="label label-warning">隐藏</span> @endif</td>
                    <td><a href="{{url('admin/category/edit',['id'=>$child['id']])}}"
                           class="btn btn-primary">编辑</a>
                        @if(!$child['is_has_goods'])
                                <button class="btn btn-primary delete" data-value="{{$child["id"]}}">
                                    删除
                                </button>
                                <button class="btn btn-primary toggle" data-value="{{$child["id"]}}"> @if($child['status'] == 1) 隐藏 @elseif($child['status'] == 2) 显示 @endif
                                </button>
                        @endif
                    </td>
                </tr>
                @if(!empty($child['children']))
                    @foreach ($child['children'] as $third)
                        <tr class="treegrid-{{$third["id"]}} treegrid-parent-{{$child["id"]}}">
                            <td>{{$third["id"]}}</td>
                            <td>{{$third["cate_name"]}}</td>
                            <td>{{$third["material"]}}</td>
                            <td>{{$third["size"]}}</td>
                            <td>@if ($third["suggest_status"] == 1)
                                    <span class="label label-success">推荐</span>
                                @else
                                    <span class="label label-danger">不推荐</span>
                                @endif</td>
                            <td>@if($third['status'] == 1)  <span class="label label-success">显示</span> @elseif ($third['status'] == 2)  <span class="label label-warning">隐藏</span> @endif</td>
                            <td><a href="{{url('admin/category/edit',['id'=>$third['id']])}}"
                                   class="btn btn-primary">编辑</a>
                                @if(!$third['is_has_show_chirld'])
                                    <button class="btn btn-primary delete"
                                            data-value="{{$third["id"]}}">
                                        删除
                                    </button>
                                    <button class="btn btn-primary toggle" data-value="{{$third["id"]}}"> @if($third['status'] == 1) 隐藏 @elseif($third['status'] == 2) 显示 @endif
                                    </button>
                                @endif
                            </td>
                        </tr>
                        @if(!empty($third['children']))
                            @foreach ($third['children'] as $fourth)
                                <tr class="treegrid-{{$fourth["id"]}} treegrid-parent-{{$third["id"]}}">
                                    <td>{{$fourth["id"]}}</td>
                                    <td>{{$fourth["cate_name"]}}</td>
                                    <td>{{$fourth["material"]}}</td>
                                    <td>{{$fourth["size"]}}</td>
                                    <td>@if ($fourth["suggest_status"] == 1)
                                            <span class="label label-success">推荐</span>
                                        @else
                                            <span class="label label-danger">不推荐</span>
                                        @endif</td>
                                    <td>@if($fourth['status'] == 1)  <span class="label label-success">显示</span> @elseif ($fourth['status'] == 2)  <span class="label label-warning">隐藏</span> @endif</td>
                                    <td><a href="{{url('admin/category/edit',['id'=>$fourth['id']])}}"
                                           class="btn btn-primary">编辑</a>
                                        <button class="btn btn-primary delete"
                                                data-value="{{$fourth["id"]}}">
                                            删除
                                        </button>
                                        <button class="btn btn-primary toggle" data-value="{{$fourth["id"]}}"> @if($fourth['status'] == 1) 隐藏 @elseif($fourth['status'] == 2) 显示 @endif
                                        </button>
                                    </td>
                                </tr>
                            @endforeach
                        @endif
                    @endforeach
                @endif
            @endforeach
        @endif
    @endforeach
</table>
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modalLabel">是否确认删除类目</h4>
            </div>
            <div class="modal-body">
                <input id="delete_id" type="hidden" value=""/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="delete">确认</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="modal-toggle" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modalLabel">是否确认?</h4>
            </div>
            <div class="modal-body">
                <input id="toggle_id" type="hidden" value=""/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="toggle">确认</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('.list').treegrid();
        $("#categoryList").delegate(".delete", "click", function () {
            $("#delete_id").val($(this).attr("data-value"));
            $('#modal').modal('show');
            return false;
        });
        $("#categoryList").delegate(".toggle", "click", function () {
            $("#toggle_id").val($(this).attr("data-value"));
            $('#modal-toggle').modal('show');
            return false;
        });
        $("#modal-toggle").delegate("#toggle", "click", function () {
            var categoryId = $("#toggle_id").val();
            $.post("{{url('admin/category/toggle')}}", {
                category_id: categoryId
            }, function (result) {
                if (result.code == 0) {
                    window.location.reload();
                } else {
                    alert("操作失败");
                }
            });
            return false;
        })
        $("#modal").delegate("#delete", "click", function () {
            var categoryId = $("#delete_id").val();
            $.post("{{url('admin/category/del')}}", {
                category_id: categoryId
            }, function (result) {
                if (result.code == 0) {
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            });
            return false;
        });
    });

</script>
